﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using DSAServer.Models;
using DSAServer.Models.Input;
using DSAServer.Models.Output;
using DSAServer.Service;
using DSAServer.Utils;

namespace DSAServer.Controllers
{
    public class ClientController : BaseController
    {
        //http://localhost:65205/api/order/GetTaobaoOrder?data={%22no%22:%221%22,%22ww%22:%22111%22}&json=true
        public ActionResult GetTaobaoOrder()
        {
            InvokeResult<List<TaobaoSunOrderInfo>> result = new InvokeResult<List<TaobaoSunOrderInfo>>();
            TaobaoOrderGetPara data = getRequestData<TaobaoOrderGetPara>();

            //判断该订单是否已经入库
            OrderService orderService = new OrderService();
            if (!orderService.ExistOrder(data.no))
            {
                #region 注释
                //List<string> tids = new List<string>();
                //tids.Add("992363314325334");
                //tids.Add("994049136828743");
                //tids.Add("993914575445744");
                //tids.Add("993030500445744");
                //tids.Add("993029465175744");
                //tids.Add("992106839235744");
                //tids.Add("892872298347463");
                //tids.Add("992202994880542");
                //tids.Add("992184519331911");
                //tids.Add("993092902925612");
                //tids.Add("891317998145080");
                //tids.Add("891303119232990");
                //tids.Add("892707733093771");
                //tids.Add("892054304630063");
                //tids.Add("893000040482376");
                //tids.Add("892980682976691");
                //tids.Add("994413205153536");
                //tids.Add("892621571922894");
                //tids.Add("892573256862092");
                //tids.Add("992814028348407");
                //tids.Add("994325041619203");
                //tids.Add("890242713914374");
                //tids.Add("892490694622467");
                //tids.Add("891830301528560");
                //tids.Add("991831474943838");
                //tids.Add("992739787153519");
                //tids.Add("892439171500486");
                //tids.Add("891016471370369");
                //tids.Add("994175442019205");
                //tids.Add("890958950154879");
                //tids.Add("993485053202915");
                //tids.Add("892599085959098");
                //tids.Add("992550262782717");
                //tids.Add("890840630695761");
                //tids.Add("892536841816765");
                //tids.Add("891592626076765");
                //tids.Add("891580946485761");
                //tids.Add("892507569414957");
                //tids.Add("891525424383988");
                //tids.Add("993226577628518");
                //tids.Add("993138731594937");
                //tids.Add("991312998072546");
                //tids.Add("891287266186371");
                //tids.Add("993061219517722");
                //tids.Add("890443273378370");
                //tids.Add("993632006302943");
                //tids.Add("992946497416039");
                #endregion

                //foreach (string tid in tids)
                //{
                TaobaoOrderInfo order = new TaobaoOrderInfo();
                TopService ts = new TopService();
                order = ts.GetTopOrder(data.no);
                if (order != null)
                {
                    if (order.buyernick == data.ww)
                    {
                        result.data = order.orders;
                        result.ok = true;
                        orderService.Insert(order);
                    }
                    else
                    {
                        result.ok = false;
                        result.msg = "该订单号不是您购买的！请确认旺旺号是否正确";
                        LogUtil.Info("订单号" + data.no + "不是" + data.ww + "购买的！请确认旺旺号是否正确");
                    }
                }
                else
                {
                    result.ok = false;
                    result.msg = "请核对您的订单号是否正确！";
                    LogUtil.Info("请核对您的订单号" + data.no + "是否正确");
                }
                //}
            }
            else
            {
                //已经入库就从库中调取
                result.data = orderService.GetOrderData(data.no, data.ww);
                result.ok = result.data != null;
                if (!result.ok)
                    result.msg = "该订单已经上传照片了，请不要重复上传";
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        public ActionResult GetPhotoSize()
        {
            InvokeResult<List<PhotoSizeInfo>> result = new InvokeResult<List<PhotoSizeInfo>>();
            result.data = new List<PhotoSizeInfo>();
            result.data.Add(new PhotoSizeInfo() { name = "5寸", width = 1.1f, height = 2.5f });

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetConfig()
        {
            InvokeResult<List<ConfigItem>> result = new InvokeResult<List<ConfigItem>>();
            result.data = new List<ConfigItem>();
            result.data.Add(new ConfigItem() { ckey = "pic_server_appid", cvalue = "1" });
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult QueryPhotosize()
        {
            var result = new ManagerService().QueryPhotoSize();
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        public ActionResult SaveOrder()
        {
            InvokeResult result = new InvokeResult();
            TaobaoOrderGetPara data = getRequestData<TaobaoOrderGetPara>();
            result.msg = "上传失败!";

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SaveOrderImage()
        {
            InvokeResult result = new InvokeResult();
            ImageSubmitInfo data = getRequestData<ImageSubmitInfo>();
            OrderService orderService = new OrderService();
            result = orderService.InsertImage(data);
            return Json(result, JsonRequestBehavior.AllowGet);

        }

        //public ActionResult UpdateTrade()
        //{
        //    InvokeResult result = new InvokeResult();
        //    string no = getRequestData<string>();
        //    OrderService orderService = new OrderService();
        //    result = orderService.UpdateTrade(no);
        //    return Json(result, JsonRequestBehavior.AllowGet);
        //}

        public ActionResult UpdateTrade()
        {
            InvokeResult result = new InvokeResult();
            try
            {
                object[] obj = getRequestData<object[]>();
                OrderService orderService = new OrderService();
                result = orderService.UpdateTrade((string)obj[0], JsonHelper.DeserializeObject<List<ImageSubmitInfo>>(obj[1].ToString()));
            }
            catch (Exception ex)
            {
                LogUtil.Info(DateTime.Now + ":" + ex.Message);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

    }
}